package com.dingreading.cloud.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.admin.dto.StaffDto;
import com.dingreading.cloud.admin.dto.StaffRolesDto;
import com.dingreading.cloud.admin.entity.Staff;
import com.dingreading.cloud.admin.service.StaffRolesService;
import com.dingreading.cloud.admin.service.StaffService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/staff")
@Api(tags = "总部-门店员工管理")
public class StaffController {

    @Resource
    private StaffService staffService;
    @Resource
    private StaffRolesService staffRolesService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid", required = false) String storeUid,
            @ApiParam(value = "员工账号/员工名/手机号") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "启用/禁用 0.禁用 1.启用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        return staffService.pageList(pageUtil, storeUid, keyword, enabled);
    }


    @ApiOperation(value = "获取员工信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "员工uid") @RequestParam(value = "uid") String uid
    ) {
        Staff data = staffService.getByUid(uid);
        if (null == data) return R.fail("员工不存在");

        return R.ok(data);
    }


    @ApiOperation(value = "编辑员工信息")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "员工uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "全名") @RequestParam(value = "fullName") String fullName,
            @ApiParam(value = "昵称") @RequestParam(value = "nickName") String nickName,
            @ApiParam(value = "工作手机号") @RequestParam(value = "pubPhone") String pubPhone,
            @ApiParam(value = "个人手机号") @RequestParam(value = "priPhone") String priPhone,
            @ApiParam(value = "活动系统主目录权限") @RequestParam(value = "subjectType", required = false) String subjectType
    ) {
        boolean l = staffService.updateInfo(headStaffUid, headStaffName, uid, fullName, nickName, pubPhone, priPhone, subjectType);
        if (!l)
            return R.fail("更新员工信息失败");
        return R.ok();
    }

    @ApiOperation(value = "获取所有门店的店长")
    @PostMapping(value = "/storeManagerPages")
    public R<Object> storeManagerPages(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店名") @RequestParam(value = "keyword", required = false) String keyword
    ) {
        PageDto<StaffDto> page = staffService.storeManagerPageList(pageUtil, keyword);
        return R.ok(page);
    }


    @ApiOperation(value = "根据关键词查询门店的店长")
    @PostMapping(value = "/getStoreManager")
    public R<Object> getStoreManager(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店名/店长名/员工账号") @RequestParam(value = "keyword") String keyword
    ) {
        if (StringUtils.isBlank(keyword))
            return R.fail("请先输入关键词查询");
        if (keyword.length() < 2)
            return R.fail("请输入至少2个关键词查询");

        List<StaffDto> list = staffService.getStoreManager(keyword);

        return R.ok(list);
    }


    @ApiOperation(value = "获取门店的员工")
    @PostMapping(value = "/getStoreStaffs")
    public R<Object> getStoreStaffs(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "员工名/员工账号/手机号") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "启用/禁用 0.禁用 1.启用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        List<StaffDto> list = staffService.getStoreStaffs(storeUid, keyword, enabled);

        long count = 0;
        List<String> roleNumList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            Map<String, Long> roleMap = list.stream().collect(Collectors.groupingBy(StaffDto::getRoleName, Collectors.counting()));
            for (Map.Entry<String, Long> entry : roleMap.entrySet()) {
                count += entry.getValue();
                String msg = entry.getKey() + ":" + entry.getValue();
                roleNumList.add(msg);
            }
        }
        roleNumList.add(0, "账号总数:" + count);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("staffList", list);
        jsonObject.put("roleNumList", roleNumList);
        return R.ok(jsonObject);
    }


    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = staffService.updateEnabled(headStaffUid, headStaffName, staffUid, enabled);
        if (!l)
            return R.fail("更新状态失败");

        // 更新角色权限
        staffRolesService.updateEnabledByStaffUid(headStaffUid, headStaffName, staffUid, enabled);
        return R.ok();
    }


    @ApiOperation(value = "删除员工")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid
    ) {
//        long l = staffService.dropBean(staffUid);
//        if (l <= 0)
//            return R.fail("删除失败");
//        // 删除所有角色
//        staffRolesService.deleteByStaffUid(staffUid);
//        return R.ok();
        return R.fail("此功能已关停");
    }


    @ApiOperation(value = "重置员工密码")
    @PostMapping(value = "/resetPwd", produces = "application/json; charset=UTF-8")
    public R<Object> resetPwd(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid
    ) {
        Staff staff = staffService.getByUid(staffUid);
        if (null == staff) return R.fail("员工获取失败");

        // 设置员工密码
        String username = staff.getUsername();
        String pwdSalt = KitUtil.getRandomStr(10);
        String pwd = username + KitUtil.getRandomPwdStr(4);
        String pwdSource = StaffPwdUtil.getPwdSource(pwd, pwdSalt);
        String encodedPwd = MD5Util.MD5Encode(pwdSource);

        boolean l = staffService.resetPwd(headStaffUid, headStaffName, staffUid, encodedPwd, pwdSalt);
        if (!l) return R.fail("重置密码失败");

        return R.ok("账号：" + username + " 密码：" + pwd);
    }

    @ApiOperation(value = "重置门店所有员工的密码")
    @PostMapping(value = "/resetStoreStaffPwd", produces = "application/json; charset=UTF-8")
    public R<Object> resetStoreStaffPwd(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid
    ) {
        List<StaffDto> staffList = staffService.getStoreStaffs(storeUid, null, null);
        if (CollectionUtils.isEmpty(staffList))
            return R.fail("员工获取失败");

        Map<String, List<String>> pwdMap = new HashMap<>();
        Map<String, List<StaffDto>> staffMap = staffList.stream().collect(Collectors.groupingBy(StaffDto::getRoleName));
        for (Map.Entry<String, List<StaffDto>> entry : staffMap.entrySet()) {
            String roleName = entry.getKey();
            List<StaffDto> list = entry.getValue();

            List<String> pwdList = new ArrayList<>();
            for (StaffDto staff : list) {
                // 设置员工密码
                String username = staff.getUsername();
                String pwdSalt = KitUtil.getRandomStr(10);
                String pwd = username + KitUtil.getRandomPwdStr(4);
                String pwdSource = StaffPwdUtil.getPwdSource(pwd, pwdSalt);
                String encodedPwd = MD5Util.MD5Encode(pwdSource);

                boolean l = staffService.resetPwd(headStaffUid, headStaffName, staff.getStaffUid(), encodedPwd, pwdSalt);
                if (!l)
                    return R.fail("重置密码失败");

                pwdList.add("账号：" + username + " 密码：" + pwd);
            }
            pwdMap.put(roleName, pwdList);
        }

        return R.ok(pwdMap);
    }

    @ApiOperation(value = "获取该员工所有角色信息")
    @PostMapping(value = "/getStaffRole", produces = "application/json; charset=UTF-8")
    public R<Object> getStaffRole(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid
    ) {
        List<StaffRolesDto> roleList = staffRolesService.getByStaffUid(staffUid);
        return R.ok(roleList);
    }

    @ApiOperation(value = "获取当前信息")
    @PostMapping(value = "/getRoleBean", produces = "application/json; charset=UTF-8")
    public R<Object> getRoleBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "id") Long id
    ) {
        StaffRolesDto data = staffRolesService.getByIdDto(id);
        return R.ok(data);
    }

    @ApiOperation(value = "更新角色启用状态")
    @PostMapping(value = "/updateEnabledRole", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabledRole(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        return staffRolesService.updateEnabled(headStaffUid, headStaffName, id, enabled);
    }

    @ApiOperation(value = "新增/编辑角色信息")
    @PostMapping(value = "/editRoleBean", produces = "application/json; charset=UTF-8")
    public R<Object> editRoleBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "角色名") @RequestParam(value = "roleName") String roleName,
            @ApiParam(value = "员工角色集合") @RequestParam(value = "roleList") List<Integer> roleList,
            @ApiParam(value = "是否有管理界面 0.否 1.是") @RequestParam(value = "isHeadquarterStaff", required = false) Integer isHeadquarterStaff,
            @ApiParam(value = "是否是门店员工 0.否 1.是") @RequestParam(value = "isStoreStaff", required = false) Integer isStoreStaff,
            @ApiParam(value = "是否在门店代课 0.否 1.是") @RequestParam(value = "inStoreService", required = false) Integer inStoreService

    ) {
        return staffRolesService.editRoleBean(
                headStaffUid, headStaffName, id, storeUid, staffUid,
                roleName, roleList, isHeadquarterStaff, isStoreStaff, inStoreService
        );
    }

    @ApiOperation(value = "更新员工信息和角色信息")
    @PostMapping(value = "/editStaffAndRole", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "员工uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "全名") @RequestParam(value = "fullName", required = false) String fullName,
            @ApiParam(value = "昵称") @RequestParam(value = "nickName", required = false) String nickName,
            @ApiParam(value = "工作手机号") @RequestParam(value = "pubPhone", required = false) String pubPhone,
            @ApiParam(value = "个人手机号") @RequestParam(value = "priPhone", required = false) String priPhone,
            @ApiParam(value = "活动系统主目录权限") @RequestParam(value = "subjectType", required = false) String subjectType,
            @ApiParam(value = "角色名称") @RequestParam(value = "roleName") String roleName,
            @ApiParam(value = "员工角色集合") @RequestParam(value = "roleList") List<Integer> roleList,
            @ApiParam(value = "是否有管理界面 0.否 1.是") @RequestParam(value = "isHeadquarterStaff", required = false) Integer isHeadquarterStaff,
            @ApiParam(value = "是否是门店员工 0.否 1.是") @RequestParam(value = "isStoreStaff", required = false) Integer isStoreStaff,
            @ApiParam(value = "是否在门店代课 0.否 1.是") @RequestParam(value = "inStoreService", required = false) Integer inStoreService
    ) {
        return staffService.editStaffAndRole(
                headStaffUid, headStaffName, storeUid, uid, fullName, nickName, pubPhone, priPhone, subjectType,
                roleName, roleList, isHeadquarterStaff, isStoreStaff, inStoreService
        );
    }


}